
<template>
  <div class="init-house">
    <div class="form">
      <van-form>

        <van-field readonly clickable name="picker" :value="addobj.areaname" label="池塘" placeholder="点击选择"
          @click="showPicker1 = true" />
        <van-field name="picker" v-model="addobj.typename" label="物料" placeholder="请输入">
          <template #button>
            <van-button size="small" type="primary" @click="showPicker2 = true"
              style="background-color:rgb(77, 173, 249);border:0px">选择</van-button>
          </template>

        </van-field>
        <van-field name="picker" v-model="addobj.unit" label="单位" placeholder="请输入" autocomplete="off">
          <template #button>
            <van-button size="small" type="primary" style="background-color:rgb(77, 173, 249);border:0px"
              @click="showPicker3 = true">选择</van-button>
          </template>

        </van-field>
        <van-field v-model="addobj.weight" type="number" label="数量" placeholder="数量" />
      </van-form>


      <div class="btns">

        <van-button name="2" type="info"
          style="width:80px;height:40px;background-color: #f99c79;font-size: 12px;border: 0px;" @click="cheng()"
          class="btn">电子秤</van-button>

        <van-button name="2" class="btn" type="primary" @click="add()" style="background: rgb(62, 150, 232);">提交
        </van-button>
      </div>



    </div>

    <van-popup v-model="showPicker1" position="bottom">
      <van-picker show-toolbar :columns="arealist" @confirm="onConfirm1" @cancel="showPicker1 = false" />
    </van-popup>

    <van-popup v-model="showPicker2" position="bottom">
      <van-picker show-toolbar :columns="typelist" @confirm="onConfirm2" @cancel="showPicker2 = false" />
    </van-popup>

    <van-popup v-model="showPicker3" position="bottom">
      <van-picker show-toolbar :columns="unitlist" @confirm="onConfirm3" @cancel="showPicker3 = false" />
    </van-popup>

  </div>
</template>

<script>
import { warehouseAdd } from "../../../api/warehouse";
import { areaList } from "../../../api/area";
import { getConfig } from "../../../api/index";
export default {
  name: "",
  data() {
    return {
      items: [],
      page: 1,
      rows: 4,
      pages: 0,
      count: 0,
      addobj: {
        areaname: "",
        typename: "",
        unit: "",
        weight: ""
      },
      showPicker1: false,
      showPicker2: false,
      showPicker3: false,
      arealist: [],
      typelist: [],
      unitlist: []
    };
  },
  methods: {

    onConfirm1(value) {
      console.log("value", value);
      this.addobj.areaname = value.text;
      this.addobj.aid = value.value;
      this.showPicker1 = false;
    },
    onConfirm2(value) {
      this.addobj.typename = value;
      this.showPicker2 = false;
    },
    onConfirm3(value) {
      this.addobj.unit = value;
      this.showPicker3 = false;
    },
    async init() {

      let config = await getConfig();
      if (config.status == 200) {
        var data = config.data;
        this.typelist = data.typelist.split(/[(\r\n)\r\n]+/);
        this.unitlist = data.unit.split(/[(\r\n)\r\n]+/);

      }
    },
    async getarea() {
      var res = await areaList({ userid: localStorage.getItem("userid") });
      if (res.status == 200) {
        var list = res.data;
        let arealist = [];
        for (var i = 0; i < list.length; i++) {
          arealist.push({ text: list[i].areaname, value: list[i].id });
        }

        this.arealist = arealist;
      }


    },
    cheng() {
      try {
        window.ReactNativeWebView.BluetoothConn();
      } catch (eee) {

      }
    },
    async add() {
      var data = {};

      if (this.addobj.areaname == "") {
        this.$toast("请选择池塘");
        return;
      }
      if (this.addobj.typename == "") {
        this.$toast("请选择物料");
        return;
      }
      if (this.addobj.unit == "") {
        this.$toast("请选择单位");
        return;
      }
      if (this.addobj.weight == "") {
        this.$toast("请输入数量");
        return;
      }



      this.addobj.userid = localStorage.getItem("userid");
      this.addobj.type = 2;
      let res = await warehouseAdd(this.addobj);
      if (res.status == 200) {
        this.$toast("出库成功");
        this.addobj = {};

      }
    },
    setData(num) {
      this.addobj.weight = num;
    }
  },
  computed: {},
  watch: {},
  created() {
    this.init();
    this.getarea();
  },
  mounted() {



  },
};
</script>

<style lang="scss" scoped>
.init-house {
  width: 100%;
  height: 100%;

  .form {
    width: 100%;
    background-color: #fff;
    padding: 40px 30px;

    .btns {
      margin-top: 100px;
      width: 100%;
      display: flex;
      justify-content: space-around;

      .btn {
        border-radius: 15px;
      }
    }
  }
}
</style>